<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class seo extends CI_Model {

    var $seo = array();
    public $mytable = "seo";

    function __construct() {
        parent::__construct();
        $seo['title'] = '';
        $seo['h1'] = '';
        $seo['description'] = '';
        $seo['keyword'] = '';
    }

    function set($arr_set, $page = 0) {
        $arr = array();
        $add = "";
        if ($page > 1)
            $add = " - Trang " . $page;
        $arr['title'] = @$arr_set->SEO_METATITLE . $add.' '.$this->config->item('SITE_NAME');
        $arr['h1'] = @$arr_set->SEO_H1;
        $arr['description'] = @$arr_set->SEO_DESCRIPTION . $add.' - '.$this->config->item('SITE_DESCRIPTION');
        $arr['keyword'] = @$arr_set->SEO_KEYWORD;
        return array_merge($this->seo, $arr);
    }

    function set_title($title = '') {
        $this->seo['title'] = $title.' '.$this->config->item('SITE_NAME');
    }

    function set_h1($h1 = '') {
        $this->seo['h1'] = $h1;
    }

    function set_description($description = '') {
        $this->seo['description'] = $description.' '.$this->config->item('SITE_DESCRIPTION');
    }

    function set_keyword($keyword = '') {
        $this->seo['keyword'] = $keyword;
    }

    function result() {
        return $this->seo;
    }

    function insert($seo_code = '', $arr_insert) {
        $arr_default = array();
        $arr_default['SEO_CODE'] = $seo_code;
        $arr_default['SEO_METATITLE'] = '';
        $arr_default['SEO_H1'] = '';
        $arr_default['SEO_KEYWORD'] = '';
        $arr_default['SEO_DESCRIPTION'] = '';
        $arr_insert = array_merge($arr_default, $arr_insert);
        $this->db->insert($this->mytable, $arr_insert);
        return $this->db->insert_id();
    }

    function get($seo_code = '', $page = 0) {
        $this->db->from($this->mytable);
        $this->db->where('SEO_CODE', $seo_code);
        $seo = $this->db->get()->first_row();
        if (!$seo) {
            $this->insert($seo_code, array());
            $this->get($seo_code);
            return $this->seo;
        }
        $add = "";
        if ($page > 1)
            $add = " - Trang " . $page;

        $this->seo['title'] = $seo->SEO_METATITLE . $add.' '.$this->config->item('SITE_NAME');
        $this->seo['h1'] = $seo->SEO_H1;
        $this->seo['keyword'] = $seo->SEO_KEYWORD;
        $this->seo['description'] = $seo->SEO_DESCRIPTION . $add.' '.$this->config->item('SITE_DESCRIPTION');
        return $this->seo;
    }

    function decode_tag_html($content) {
      //  $content = $this->en_bbcode($content);
        return htmlspecialchars_decode($content);
        return htmlspecialchars($content);
    }

    function encode_tag_html($content) {
        $content = trim($content);
        $content = htmlspecialchars_decode(html_entity_decode($content, ENT_QUOTES, 'UTF-8'), ENT_QUOTES);
        return htmlspecialchars($content, ENT_QUOTES);
    }
 

    function seolink($str = '') {
        if (!$str)
            return false;
        return $this->tmd_string_change_url_class($str);
        $unicode = array(
            'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
            'd' => "đ",
            'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
            'i' => 'í|ì|ỉ|ĩ|ị',
            'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
            'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
            'y' => 'ý|ỳ|ỷ|ỹ|ỵ',
            'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
            'D' => "Đ",
            'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
            'I' => 'Í|Ì|Ỉ|Ĩ|Ị',
            'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
            'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
            'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
        );

        foreach ($unicode as $nonUnicode => $uni)
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);

        $str = trim($str);
        $str = strtoupper(substr($str, 0, 1)) . substr($str, 1, strlen($str));
        return $str;
    }
	
	function curPageURL() {
        $pageURL = 'http';
        if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
            $pageURL .= "s";
        }
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }
	
    function tmd_string_change_url_class($sInput) {
        $badChars = array("á|a", "à|a", "ả|a", "ã|a", "ạ|a", "â|a", "ấ|a", "ầ|a", "ẩ|a", "ẫ|a", "ậ|a", "ă|a", "ắ|a", "ằ|a", "ẳ|a", "ẵ|a", "ặ|a", "đ|d", "é|e", "è|e", "ẻ|e", "ẽ|e", "ẹ|e", "ê|e", "ế|e", "ề|e", "ể|e", "ễ|e", "ệ|e", "í|i", "ì|i", "ỉ|i", "ĩ|i", "ị|i", "ó|o", "ò|o", "ỏ|o", "õ|o", "ọ|o", "ô|o", "ố|o", "ồ|o", "ổ|o", "ỗ|o", "ộ|o", "ơ|o", "ớ|o", "ờ|o", "ở|o", "ỡ|o", "ợ|o", "ú|u", "ù|u", "ủ|u", "ũ|u", "ụ|u", "ư|u", "ứ|u", "ừ|u", "ử|u", "ữ|u", "ự|u", "ý|y", "ỳ|y", "ỷ|y", "ỹ|y", "ỵ|y", "Á|A", "À|A", "Ả|A", "Ã|A", "Ạ|A", "Â|A", "Ấ|A", "Ầ|A", "Ẩ|A", "Ẫ|A", "Ậ|A", "Ă|A", "Ắ|A", "Ằ|A", "Ẳ|A", "Ẵ|A", "Ặ|A", "Đ|D", "É|E", "È|E", "Ẻ|E", "Ẽ|E", "Ẹ|E", "Ê|E", "Ế|E", "Ề|E", "Ể|E", "Ễ|E", "Ệ|E", "Í|I", "Ì|I", "Ỉ|I", "Ĩ|I", "Ị|I", "Ó|O", "Ò|O", "Ỏ|O", "Õ|O", "Ọ|O", "Ô|O", "Ố|O", "Ồ|O", "Ổ|O", "Ỗ|O", "Ộ|O", "Ơ|O", "Ớ|O", "Ờ|O", "Ở|O", "Ỡ|O", "Ợ|O", "Ú|U", "Ù|U", "Ủ|U", "Ũ|U", "Ụ|U", "Ư|U", "Ứ|U", "Ừ|U", "Ử|U", "Ữ|U", "Ự|U", "Ý|Y", "Ỳ|Y", "Ỷ|Y", "Ỹ|Y", "Ỵ|Y", "/|-", " |-", "---|-", "--|-", "%|", ",|-", "–|");
        $size = count($badChars);
        for ($i = 0; $i < $size; $i++) {
            $test = explode("|", $badChars[$i]);
            $in = $test[0];
            $out = $test[1];
            $sInput = str_replace($in, $out, $sInput);
        }
        $sInput = trim(strtolower($sInput));
        $sInput = str_replace("'", "", $sInput);
        $sInput = str_replace("/", "", $sInput);
        $sInput = str_replace("|", "", $sInput);
        $sInput = str_replace("\\", "", $sInput);
        $sInput = str_replace("*", "", $sInput);
        $sInput = str_replace("&", "", $sInput);
        $sInput = str_replace('?', "", $sInput);
        $sInput = str_replace('!', "", $sInput);
        $sInput = str_replace(",", "", $sInput);
        $sInput = str_replace(";", "", $sInput);
        $sInput = str_replace("%", "-", $sInput);
        $sInput = str_replace("$", "-", $sInput);
        $sInput = str_replace("=", "-", $sInput);
        $sInput = str_replace("(", "-", $sInput);
        $sInput = str_replace(")", "-", $sInput);
        $sInput = str_replace('"', "", $sInput);
        $sInput = str_replace(':', "-", $sInput);
        $sInput = str_replace('.', "-", $sInput);
        $sInput = str_replace('[', "", $sInput);
        $sInput = str_replace(']', "", $sInput);
        $sInput = str_replace('{', "", $sInput);
        $sInput = str_replace('}', "", $sInput);
        $arrayRemove = array( ".." => ".", ",," => ",", '"' => "", '"' => "");
        foreach ($arrayRemove as $key => $row)
            while (strpos($sInput, $key))
                $sInput = str_replace($key, $row[$key], $sInput);
		
		$sInput = str_replace("--", "-", $sInput);
        if (substr($sInput, 0, 1) == "-")
            $sInput = substr($sInput, 1, strlen($sInput));
        if (substr($sInput, strlen($sInput) - 1, 1) == "." || substr($sInput, strlen($sInput) - 1, 1) == "," || substr($sInput, strlen($sInput) - 1, 1) == "-")
            $sInput = substr($sInput, 0, strlen($sInput) - 1);
        $text = preg_replace('~[^\\pL\d]+~u', '-', $sInput);

        // trim
        $text = trim($text, '-');

        // transliterate
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

        // lowercase
        $text = strtolower($text);

        // remove unwanted characters
        $text = preg_replace('~[^-\w]+~', '', $text);

        if (empty($text)) {
            return 'n-a';
        }
        return $text;
        // default not return strtolower
    }
	 function write_breadcrums($array_links = array()) {
        $ret = '';
        $first = true;
        $slash_div = "";
        $i = 0;
        foreach ($array_links as $array_link):
            if ($first == true):
                $first = false;
                $ret.='<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb" >';
                $ret.='		<a title="' . $array_link["title"] . '" href="' . $array_link["link"] . '" itemprop="url">';
                $ret.='			<span itemprop="title">' . $array_link["text"] . '</span>';
                $ret.='		</a>';
            else:
                $ret.='				<span style="float:left">&nbsp;&nbsp;›&nbsp;&nbsp;</span>';
                $ret.='				<div itemprop="child" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">';
                $ret.='					<a title="' . $array_link["title"] . '" href="' . $array_link["link"] . '" itemprop="url">';
                $ret.='						<span itemprop="title">' . $array_link["text"] . '</span></a>';
            endif;
            $slash_div.="</div>";
        endforeach;
        return $ret . $slash_div;
    }
	function DecimalToUni($str) {

$convmap = array(
  0xe1,0xe1,0x0,0xe1,0xc1,0xc1,0x0,0xc1,0xe0,0xe0,0x0,0xe0,0xc0,0xc0,0x0,0xc0,0x1ea3,0x1ea3,0x0,0x1ea3
,0x1ea2,0x1ea2,0x0,0x1ea2,0xe3,0xe3,0x0,0xe3,0xc3,0xc3,0x0,0xc3,0x1ea1,0x1ea1,0x0,0x1ea1,0x1ea0,0x1ea0,0x0,0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